package Messages;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import ocsf.server.ConnectionToClient;
import server.SQLConnection;
/**
 * this class implements the librarian get book from db
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */
public class GetBook {
	String msg = null;
	private ConnectionToClient client ;
	public String bookid,title;
	public String getBookid() {
		return bookid;
	}
	/**
	 * set bookid
	 * @param bookid
	 */
	public void setBookid(String bookid) {
		this.bookid = bookid;
	}
	/**
	 * get book title
	 * @return
	 */
	public String getTitle() {
		return title;
	}
	/**
	 * 
	 * set book title
	 * @param title
	 */
	public void setTitle(String title) {
		this.title = title;
	}
	/**
	 * 
	 * @param client
	 */
	public GetBook(ConnectionToClient client) {
		super();
		this.client = client;
	}
	/**
	 * this sql query return from db the wanted book
	 * @param str
	 */
	public Object getbook(String[] str){
		
		
		try 
		{

			System.out.println("getting book detail for viewing");
			Statement stmt;
			SQLConnection conn = new  SQLConnection();
			stmt = conn.getConnection().createStatement();
			
			ResultSet rs= stmt.executeQuery("SELECT * FROM db1.bookscopies WHERE CopyId='"+str[1]+"';");
				try {
				
						if (rs.next())
							{
							      
								bookid=rs.getString(1);
								title=rs.getString(2);
							    ArrayList<String> ViewArr=new ArrayList<String>();
								ViewArr.add("GetBook");
						        ViewArr.add(bookid);
						        ViewArr.add(title);	
								//client.sendToClient(ViewArr);
						        return ViewArr;
															}
						else 
							{
							msg="Book ID Doesn't Exsists";
							return msg;
							//client.sendToClient("Book ID Doesn't Exsists");							
							}
  
					}
						catch (SQLException e)
								{	
								e.printStackTrace();
								}
		}
  
		catch (Exception ex )
		{
	  ex.printStackTrace();
		}
		return msg;
		
		
	}
}
